import request from "@/utils/request";
import router from "@/router";
import { Message } from 'element-ui';
export default {
    namespaced: true,
    state: {
        userInfo: JSON.parse(localStorage.getItem('user')) || {},
        token: JSON.parse(localStorage.getItem('token')) || ''
    },
    actions: {

    },
    mutations: {
        // 用户登录 返回token
        userLogin(state, form) {
            request.post('/user/login', form).then(res => {
                if (res.code == 20000) {
                    state.token = res.data.token
                    localStorage.setItem('token', JSON.stringify(state.token))
                    this.commit('userStore/getUserInfo')

                    Message({
                        type: 'success',
                        message: '用户登陆成功~'
                    });
                } else {
                    Message({
                        type: 'error',
                        message: res.msg
                    });
                }
            })
        },
        // 获取用户信息
        getUserInfo(state) {
            request.get('/user/userInfo').then(res => {
                if (res.code == 20000) {
                    state.userInfo = res.data
                    localStorage.setItem('user', JSON.stringify(state.userInfo))
                    // this.commit('userStore/saveUserInfo')
                }
            })
        },
        // 持久化存储用户信息
        saveUserInfo(state) {
            // state.userInfo = JSON.parse(localStorage.getItem('user'))
        },
        //修改用户信息
        updateUserInfo(state, newData) {
            // newData.balance = Number(state.userInfo.balance) + Number(newData.balance)
            request.post('/user/updateMyInfo', newData).then(res => {
                if (res.code == 20000) {
                    // localStorage.removeItem('user')
                    this.commit('userStore/getUserInfo')
                }
            })
        },
        // 修改密码
        updatePassword(state, pwdForm) {
            request.post('/user/updatePassword', pwdForm).then(res => {
                if (res.code == 20000) {
                    Message({
                        type: 'success',
                        message: '密码修改成功~'
                    });
                } else {
                    Message({
                        type: 'error',
                        message: res.msg
                    });
                }
            })
        },
        // 退出登录
        loginout(state) {
            request.post('/user/logout', {}).then(res => {
                if (res.code == 20000) {
                    // 判断当前路由
                    if (router.currentRoute.path !== '/home') {
                        router.push('/home')
                    }
                    state.token = ''
                    state.userInfo = {}
                    localStorage.clear()
                }
            })


        },
        // 充值
        balanceAdd(state, balance) {
            request.put('/user/balanceAdd?balance=' + balance).then(res => {
                if (res.code == 20000) {
                    this.commit('userStore/getUserInfo')
                    Message({
                        type: 'success',
                        message: '充值成功~'
                    });
                }
            })
        },
        // 用户注册
        userRegister(state, form) {
            request.post('/user/register', form).then(res => {
                if (res.code == 20000) {
                    Message({
                        type: 'success',
                        message: '用户注册成功'
                    })
                }
            })
        }

    },
    getters: {

    }
}